package com.mqttProvider.task;

import com.alibaba.fastjson.JSON;
import com.mqttProvider.config.MqttConfig;
import com.mqttProvider.domain.GainCoreDTO;
import com.mqttProvider.domain.PersonnelOrientationVO;
import com.mqttProvider.domain.RangingCoreDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author chenming
 * @description
 * @create: 2022-11-24
 */
@Component
@EnableScheduling
public class ProviderTask {
    @Autowired
    private MqttConfig mqttConfig;

    public static final String TOF_TOPIC = "/EH100602/dev/bs/tof/dist/";

    @Scheduled(cron = "0/1 * * * * ?")
    public void test() {

        PersonnelOrientationVO personnelOrientationVO = new PersonnelOrientationVO();
        personnelOrientationVO.setEmpId(5845L);
        personnelOrientationVO.setStationNumber("40190");
        personnelOrientationVO.setX(BigDecimal.valueOf(1964.50251));
        personnelOrientationVO.setY(BigDecimal.valueOf(4943.31023));
        personnelOrientationVO.setZ(BigDecimal.valueOf(1097.90000));
        personnelOrientationVO.setLocalCardNumber(92172);
        personnelOrientationVO.setRangeDist(83.09);
        personnelOrientationVO.setDirection(2);

        GainCoreDTO gainCoreDTO = new GainCoreDTO();
        gainCoreDTO.setAll_bs_num(1);
        gainCoreDTO.setBs_addr(Long.valueOf(personnelOrientationVO.getStationNumber()));
        gainCoreDTO.setRefresh_rate(1D);

        Map<String, RangingCoreDTO> rList = new HashMap<>();
        RangingCoreDTO rangingCoreDTO = new RangingCoreDTO();
        rangingCoreDTO.setBetween_antenna(0);
        rangingCoreDTO.setChannel(personnelOrientationVO.getDirection());
        rangingCoreDTO.setRange_bs_addr(gainCoreDTO.getBs_addr());
        rangingCoreDTO.setRange_dist(personnelOrientationVO.getRangeDist().intValue());
        rangingCoreDTO.setRssi(10);
        rangingCoreDTO.setTime_calibrate(1);
        rList.put(personnelOrientationVO.getStationNumber(), rangingCoreDTO);

        gainCoreDTO.setRlist(rList);
        gainCoreDTO.setTag_id(Long.valueOf(personnelOrientationVO.getLocalCardNumber()));
        long timeStamp = System.currentTimeMillis() / 1000;
        gainCoreDTO.setTimestamp(timeStamp);
        gainCoreDTO.setTof_sn((int) timeStamp % 1000000);

        mqttConfig.publish(TOF_TOPIC + personnelOrientationVO.getLocalCardNumber(), JSON.toJSONString(gainCoreDTO), 1, false);

    }
}
